package com.wf.config;

import com.wf.propertity.JwtProperties;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


@Configuration
public class FeignConfig implements RequestInterceptor {

    @Autowired
    JwtProperties jwtProperties;

//    @Bean
//    public Decoder feignDecoder() {
//        return new ResponseEntityDecoder(new SpringDecoder(feignHttpMessageConverter()));
//    }

//    private ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() {
//        final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(new ByteArrayHttpMessageConverter()
//                , new MappingJackson2HttpMessageConverter());
//        return () -> httpMessageConverters;
//    }

    @Bean
    public Logger.Level logLevel() {
        return Logger.Level.FULL;
    }



    @Override
    public void apply(RequestTemplate template) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            // 添加token
            String header = request.getHeader(jwtProperties.getAdminTokenName());
            template.header(jwtProperties.getAdminTokenName(), header);
        }
    }
}
